<?php

namespace App\Admin\Metrics\Examples;

use App\Models\Phone;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Http\Request;
use App\Models\Api;
use Illuminate\Support\Facades\DB;

class TypeYestodayPhones extends Bar
{

    protected $typedata;
    protected $typecolor;
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();
        $color = Admin::color();
        $dark35 = $color->dark35();

        $this->typedata = Phone::query()->with(['typedel'])->select('type_id',DB::raw('count(*) as num'))->where('datenum','=',date('Ymd',time()-(24*60*60)))->where('type_id','<>',0)->groupBy('type_id')->get()->toArray();
        $this->typecolor =[
            $color->link(),
            $color->info(),
            $color->success(),
            $color->danger(),
            $color->warning(),
            $color->indigo(),
            $color->blue(),
            $color->red(),
            $color->orange(),
            $color->green(),
        ];

        // 卡片内容宽度
        $this->contentWidth(11, 1);
        // 标题
        $this->title('分类型统计(昨日新增)');
        // 设置下拉选项
        $this->dropdown([]);
        // 设置图表颜色
        //$this->chartColors($this->typecolor);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $data = [];
        foreach ($this->typedata as $val){
            $data[] = $val['num'];
        }
        switch ($request->get('option')) {
            default:
                // 卡片内容
                $this->withContent();

                // 图表数据
                $this->withChart([
                    [
                        'name' => '总数据',
                        'data' => [],
                    ],
                ]);
        }
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $title
     * @param string $value
     * @param string $style
     *
     * @return $this
     */
    public function withContent()
    {
        // 根据选项显示
        $label = strtolower(
            $this->dropdown[request()->option] ?? 'last 7 days'
        );
        $apidata = Phone::query()->select('api_id','type_id',DB::raw('count(*) as num'))->where('datenum','=',date('Ymd',time()-(24*60*60)))->groupBy('api_id')->get()->toArray();

        $minHeight = '183px';
        $style = 'margin-bottom: 8px;word-break:keep-all;word-wrap:overflow-wrap;white-space: pre-line;';
        $labelWidth = 120;
        $html = "";
        $typecolor = $this->typecolor;
        array_unshift($typecolor,"#586cb1");
        $i = 0;
        foreach ($this->typedata as $val){
            $api_html = "";
            foreach ($apidata as $api){
                if($api['type_id'] == $val['type_id']){
                    $apiobj = Api::getApi($api['api_id']);
                    $api_html .= '<div class="" style="width: 150px; float: left">'.$apiobj->webname.':'.$api['num'].'</div>';
                }
            }

            $i++;
            $html .= <<<HTML
    <div class="d-flex pl-1 pr-1 pt-1" style="">
        <div style="width: {$labelWidth}px">
            <i class="fa fa-circle text-primary" style="color: {$typecolor[$i]} !important"></i> {$val['typedel']['name']}
        </div>
        <div>
            {$val['num']}
        </div>
    </div>
    <div class="pl-1" style="$style">$api_html</div>
HTML;

        }

        return $this->content(
            <<<HTML
<style>
.Dcat_Admin_Widgets_ApexCharts_Chart{ display: none !important;}
</style>
<div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;">
    $html
</div>
HTML
        );
    }
}
